require('dotenv').config();
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');

const app = express();

// 中间件
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

// 测试路由
app.get('/', (req, res) => {
  res.send('Three.js 后端服务运行中');
});

app.use(express.static('./static'));
app.use(express.static('./json'));
// 错误处理
app.use((err, req, res, next) => {
  console.error(err.stack);
  res.status(500).send('服务器错误!');
});

const PORT = 4000;
app.listen(PORT, () => {
  console.log(`服务器地址：http://localhost:${PORT}/`);
});